package com.example.kugou.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.example.kugou.common.time.TimeAdd;
import com.example.kugou.common.util.AlipayConfig;
import com.example.kugou.common.util.OrderCodeUtil;
import com.example.kugou.pojo.Personal_data;
import com.example.kugou.pojo.Vip;
import com.example.kugou.service.Personal_dataService;
import com.example.kugou.service.VipService;
import com.example.kugou.vo.Person_DataVO;
import com.example.kugou.vo.VipVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@Api(tags = "开通vip或者控制vip到期的方法", description = "提供vip相关的Rest API")
public class VipController {

    @Resource
    VipService vipService;

    @Resource
    Personal_dataService personal_dataService;

    public VipService getVipService() {
        return vipService;
    }

    public void setVipService(VipService vipService) {
        this.vipService = vipService;
    }

   @GetMapping("/getPersonIsVipByid")
   @ApiOperation("判断vip的查询")
    public VipVO getPersonIsVipByid(Integer id){
        VipVO vipVO=new VipVO();
         Vip vip = vipService.selectPersonIsVipByid(id);
         vipVO.setData(vip);
         return vipVO;
   }

   @ApiOperation("移除某用户vip")
   @PostMapping("/getvipremove")
   public  VipVO getvipremove(Integer id){
       VipVO vipVO=new VipVO();
       try {
           int vipid= vipService.deleteVipByid(id);
           if (vipid>0){
               vipVO.setMsg("取消会员成功");
               vipVO.setStatus(true);
           }else{
               vipVO.setMsg("取消会员失败");
               vipVO.setStatus(false);
           }
   }catch (Exception e){
        e.printStackTrace();
           vipVO.setMsg(e.getMessage());
           vipVO.setStatus(false);
    }
     return vipVO;
   }
    @ApiOperation("修改vip日期")
    @PostMapping("/getvipupdate")
    public  VipVO getvipupdate(Integer perId,Integer id,String Time){
        VipVO vipVO=new VipVO();
//        System.out.println(Time);
//        System.out.println(id);
        Personal_data personal_data = new Personal_data();
        personal_data.setPid(perId);
        personal_data.setPtype(2);
        try {
            int vipid= vipService.updateVipEndtime(Time,id);
            if (vipid>0){
                Map<String ,Object> map = personal_dataService.updatePerson_Data(personal_data);
                if(map.get("state").equals("Success")){
                    vipVO.setMsg("修改会员成功");
                    vipVO.setStatus(true);
                }
            }else{
                vipVO.setMsg("修改会员失败");
                vipVO.setStatus(false);
            }
        }catch (Exception e){
            e.printStackTrace();
            vipVO.setMsg(e.getMessage());
            vipVO.setStatus(false);
        }
        return vipVO;
    }

    @ApiOperation("创建vip对象")
    @GetMapping("/insertVip")
    public Person_DataVO insertVip(int id) {
        Person_DataVO person_dataVO = new Person_DataVO("增加失败",false);
        Vip vip = new Vip();
        vip.setPid(id);
        Date date = new Date();//获取当前的日期
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String str = df.format(date);//获取String类型的时间
        vip.setStarttime(str);
        TimeAdd.timeAddMonth(date,1);
        String time = TimeAdd.getTime();
        vip.setEndtime(time);
        vip.setEndtime(TimeAdd.getTime02());
        int count = vipService.insertVip(vip);
        if (count > 0){
            person_dataVO.setStatus(true);
            person_dataVO.setMsg("增加成功");
        }
        return person_dataVO;
    }

   @RequestMapping("/sendAlipay")
   @ApiOperation("沙箱支付vip")
    public void sendAlipay(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //设置响应的数据为html
        response.setContentType("text/html;charset=utf-8");
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
       alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = OrderCodeUtil.GetCode();
//        System.out.println("dingdanhao:"+out_trade_no);
//        //付款金额，必填
//        String total_amount = new String(request.getParameter("WIDtotal_amount").getBytes("ISO-8859-1"),"UTF-8");
       // 付款金额
       String total_amount = new String(request.getParameter("WIDtotal_amount").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        //订单名称，必填
        String subject = new String(request.getParameter("WIDsubject").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        //商品描述，可空
        String body = new String(request.getParameter("WIDbody").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result=null;
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();

            //这里请求结束

        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        //输出
        response.getWriter().write(result);
    }



    @RequestMapping("/queryPay")
    @ApiOperation("沙箱支付vip")
    public void queryPay(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        // 商户订单号
        String out_trade_no = new String(request.getParameter("out_trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

        // 支付宝交易号
        String trade_no = new String(request.getParameter("trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

        // 付款金额
        String total_amount = new String(request.getParameter("total_amount").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

//        System.out.println("支付宝的交易号"+trade_no);
//        System.out.println("商城交易号"+out_trade_no);
//        System.out.println("付款金额"+total_amount);

    }





























}
